﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Runtime.InteropServices;
using System.Drawing;

namespace Coot
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        // Use Win32 API functions for dealing with preview dialog box
		[DllImport("user32.dll")]
		private static extern bool GetClientRect(IntPtr hWnd, ref RECT rect);
		[DllImport("user32.DLL",EntryPoint="IsWindowVisible")]
		private static extern bool IsWindowVisible(IntPtr hWnd);

        private struct RECT
        {
            public int left, top, right, bottom;
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            if (e.Args.Length == 0)
            {
                StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
            }
            else if (e.Args[0] == "/s")
            {
                StartupUri = new Uri("ScreenSaver.xaml", UriKind.Relative);
            }
            else if (e.Args[0] == "/c")
            {
                StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
            }
            else if (e.Args[0] == "/p")
            {
                IntPtr parentHwnd = (IntPtr)uint.Parse(e.Args[1]);

                if (!IsWindowVisible(parentHwnd))
                    this.Shutdown();

                RECT rect = new RECT();

                if (GetClientRect(parentHwnd, ref rect))
                {
                    Graphics g = Graphics.FromHwnd(parentHwnd);
                    g.Clear(Color.Black);
                    //g.DrawString("Coot", new Font("Arial", 14), Brushes.White, ((float)rect.left + 10), ((float)rect.top + 5));
                    //g.DrawRectangle(Pens.Magenta, new Rectangle(0, 0, 151, 111));

                    g.DrawImage(Coot.Properties.Resources.coot, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
                    g.Dispose();
                }

                this.Shutdown();
            }
            else
            {
                StartupUri = new Uri("MainWindow.xaml", UriKind.Relative);
            }
        }
    }
}
